const { groupChat, User } = require("../../../models")

function createGroupChat(io, socket, data, callback) {
    groupChat.create(data).then(res => {
        data.members.forEach(async item => {
            let user = await User.findById(item);
            user.joinedGroups.push(res._id);
            user.save();
            
            // 创建群聊的发送者接收不到
            if (user.socketId != '') {
                socket.to(user.socketId).emit('joined_groupChat', { status_code: 200, info: '已加入群聊' });
            }
        });

        callback({ status_code: 200, info: '群聊创建成功' });
    })
}

module.exports = {
    createGroupChat,
};
